package com.example.book.in.controller;

import com.example.book.base.BaseController;
import com.example.book.base.Result;
import com.example.book.common.shiro.ShiroUtils;
import com.example.book.common.utils.Constants;
import com.example.book.common.utils.LocalFileUtils;
import com.example.book.domain.sys.SysUser;
import com.example.book.in.entity.BookCategory;
import com.example.book.in.service.BookCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import com.example.book.in.entity.BookInfo;
import com.example.book.in.service.BookInfoService;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author hcj
 * @date 2019-10-27 22:24:17
 */
@Controller
@RequestMapping(value = "/sys/in/bookInfo")
public class BookInfoController extends BaseController {

    @Autowired
    private BookInfoService bookInfoService;
    @Autowired
    private BookCategoryService categoryService;

    @Value("${system.profile.path}")
    private String profile;

    @Value("${system.profile.real_path}")
    private String realProfile;


    @ModelAttribute
    public BookInfo get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(id);
            return bookInfoService.get(bookInfo);
        } else {
            return new BookInfo();
        }
    }

    /**
     * 设置热门推荐
     *
     * @param id
     */
    @RequestMapping(value = "/setPopularSearches")
    @ResponseBody
    public String setPopularSearches(String id) {
        bookInfoService.setPopularSearches(id);
        return "success";
    }

    @RequestMapping(value = "/get")
    @ResponseBody
    public BookInfo getById(BookInfo bookInfo) {
        return bookInfo;
    }

    /**
     * 新增修改页面
     *
     * @param bookInfo
     * @param model
     * @return
     */
    @GetMapping("/form")
    public String form(BookInfo bookInfo, Model model) {
        if (bookInfo != null) {
            model.addAttribute("bookInfo", bookInfo);
        }
        return "in/book_info/book_info_form";
    }

    @RequestMapping(value = "/list")
    public String list(BookInfo bookInfo) {
        return "in/book_info/book_info_list";
    }

    @RequestMapping(value = "/info")
    @ResponseBody
    public List<BookInfo> info(BookInfo bookInfo) {
        return bookInfoService.findList(bookInfo);
    }


    @RequestMapping(value = "/findPage")
    @ResponseBody
    public PageInfo<BookInfo> findPage(BookInfo bookInfo) {
        PageInfo<BookInfo> pageInfo = bookInfoService.findPage(bookInfo);
        BookCategory category = null;
        for (BookInfo bi : pageInfo.getList()) {
            category = new BookCategory();
            category.setId(bi.getCategoryId());
            category = categoryService.get(category);
            if (category != null) {
                bi.setCategoryName(category.getName());
            }
        }
        return pageInfo;
    }

    /**
     * 保存书籍信息
     *
     * @param bookInfo
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(BookInfo bookInfo, MultipartFile file) throws IOException {

        if (!file.isEmpty()) {
            SysUser sysUser = ShiroUtils.getUser();
            String path = LocalFileUtils.upload(sysUser.getId(), profile, realProfile, file);
            bookInfo.setBookCover(path);
        }

        bookInfoService.save(bookInfo);
        return Result.success("保存成功");
    }

    @RequestMapping(value = "/logicalDeletion", method = RequestMethod.POST)
    @ResponseBody
    public Result logicalDeletion(@RequestParam("id") String id) {
        bookInfoService.logicalDeletion(id);
        return Result.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestParam("ids") String[] ids) {
        bookInfoService.delete(ids);
        return Result.success();
    }

}